package com.bdqn.t350.servlet;

import com.bdqn.t350.entity.Powerbattery;
import com.bdqn.t350.service.PowerbatteryService;
import com.bdqn.t350.service.PowerbatteryServiceImp;
import com.bdqn.t350.util.StringToUtil;
import com.bdqn.t350.util.UploadAliYunUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.UUID;

@WebServlet(name = "AddServlet",urlPatterns = "/AddServlet")
//标识该servlet支持文件上传功能
@MultipartConfig
public class AddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String carName = req.getParameter("carName");
        //ufile表单的字段
        Part part = req.getPart("ufile");
        //调用阿里云上传文件封装好的方法
        Map<String,String> map = UploadAliYunUtil.uploadImage(part);

        String realFileName = map.get("realFileName");
        String saveFileName = map.get("saveFileName");
        String suff = map.get("ext");

        PowerbatteryService powerbatteryService = new PowerbatteryServiceImp();
        Powerbattery powerbattery = new Powerbattery();
        powerbattery.setCarName(carName);
        powerbattery.setRealFileName(realFileName);
        powerbattery.setSaveFileName(saveFileName);
        powerbattery.setSuff(suff);
        int count = powerbatteryService.add(powerbattery);
        if(count>0){
            resp.sendRedirect("file_index.jsp");
        }else{
            //跳转到错误
        }

    }

}
